#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    # import pymel.core as pm
    import JTK.jmaya.core as jmc

except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckUVSetMap(CheckBase):
    def __init__(self):
        super(CheckUVSetMap, self).__init__()
        pass

    def check_map1_uv_set(self):
        mesh_list = cmds.ls(type='mesh', l=1)

        if not mesh_list:
            return []
        loss_uvsets_list = []
        for mesh in mesh_list:
            uvsets = cmds.polyUVSet(mesh, q=True, allUVSets=True)
            result = False
            # 检查uvset是否存在 map1
            for uv_set in uvsets:
                if uv_set == "map1":
                    result = True
                    break
            # 检查当前uvset是否为 map1
            currentUvSet = cmds.getAttr(mesh + ".currentUVSet")
            if currentUvSet != "map1":
                result = False

            if result is False:
                loss_uvsets_list.append(mesh)

        return loss_uvsets_list

    def check(self):

        result_proc = self.check_map1_uv_set()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        result_proc = self.check_map1_uv_set()
        for mesh in result_proc:
            try:
                cmds.setAttr(mesh + ".currentUVSet", "map1", type="string")
            except:
                pass
        return self.check()

